package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.PlaceOfficerItem;
import egovframework.npqs.pqi.mobile.jsoncontent.RelationDiseasePastItem;
import egovframework.npqs.pqi.mobile.jsoncontent.UrgencyLimitItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

public class RelationDiseasePestThread extends BasicThread {

	public interface RelationDiseasePestThreadCallback {
		public void OnRelationDiseasePestCompleted(boolean isNetworkConntected, boolean results, String message);
		public void OnRelationDiseasePestError(ErrorItem errorItem);
	}

	private String url;
	private RelationDiseasePestThreadCallback callback;
	private ArrayList<NameValuePair> post;
	private String value;
	
	/* 파라메터*/
	private String searchType, searchNatCd, searchOffcd, searchRecNo;
	
	public RelationDiseasePestThread(Context context
			,RelationDiseasePestThreadCallback callback
			, String searchType
			, String searchNatCd
			, String searchOffcd
			, String searchRecNo
			, String value
			, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.searchType = searchType;
		this.searchNatCd = searchNatCd;
		this.searchOffcd = searchOffcd;
		this.searchRecNo = searchRecNo;
		this.value = value;
	}

	@Override
	public void run() {
		Map<String,String> map = new HashMap<String, String>();
		map.put(Const.SEARCHTYPE, searchType);
		map.put(Const.SEARCHNATCD, searchNatCd);
		map.put(Const.SEARCHOFFCD, searchOffcd);
		map.put(Const.SEARCHRECNO, searchRecNo);
		
		String param = "";
		param = parameterReturn(Const.REQ027, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
		
		/*JSONParser jParser = new JSONParser();

		url = Const.MAIN_URL;
		post = ParameterUtil.getFirstRelationDiseaseParam(searchType, searchNatCd, searchOffcd, searchRecNo);
		
		Log.i("url","url = " + url + post);
		JSONObject json = jParser.getJSONFromUrl(url, post);*/
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnRelationDiseasePestCompleted(false, false, "통신 에러");
			return;
		}
		
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnRelationDiseasePestError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			Log.i("result","result = " + result);
			ArrayList<RelationDiseasePastItem> arrRelationItem = new ArrayList<RelationDiseasePastItem>();
			ArrayList<UrgencyLimitItem> arrUrgencyLimitItem = new ArrayList<UrgencyLimitItem>();
			
			if(result == 0){	// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				Log.i("관련병해충 data ", data);
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);
				appStatus.setArrRelationDeiseasePastItem(arrRelationItem);
				callback.OnRelationDiseasePestError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				JSONArray arrayLogin = json.getJSONArray(Const.DATA);
				Log.i("arrayLogin","arrayLogin = " + arrayLogin);
				Log.i("value","value = " + value);
				if(value.equals("3")){
					RelationDiseasePastItem relationItem = null;
					for (int i = 0; i < arrayLogin.length(); i++) {
						JSONObject prod = arrayLogin.getJSONObject(i);
						
						relationItem = new RelationDiseasePastItem();
						relationItem.setNatNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, RelationDiseasePastItem.NATNM)));
						relationItem.setResDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, RelationDiseasePastItem.RESDE)));
						relationItem.setPrdlstNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, RelationDiseasePastItem.PRDLSTNM)));
						relationItem.setBotNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, RelationDiseasePastItem.BOTNM)));
						relationItem.setViewDamDegNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, RelationDiseasePastItem.VIEWDAMDEGNM)));
						
						arrRelationItem.add(relationItem);
					}
					appStatus.setArrRelationDeiseasePastItem(arrRelationItem);
					callback.OnRelationDiseasePestCompleted(true, true, mResult);
				}else{
					UrgencyLimitItem urgencyItem = null;
					for (int i = 0; i < arrayLogin.length(); i++) {
						JSONObject prod = arrayLogin.getJSONObject(i);
						
						urgencyItem = new UrgencyLimitItem();
						urgencyItem.setNatNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, UrgencyLimitItem.NATNM)));
						urgencyItem.setPrdlstNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, UrgencyLimitItem.PRDLSTNM)));
						urgencyItem.setNatDetArear(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, UrgencyLimitItem.NATDETAREAR)));
						urgencyItem.setPrbCau(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, UrgencyLimitItem.PRBCAU)));
						urgencyItem.setPrbDate(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, UrgencyLimitItem.PRBDATE)));
						
						arrUrgencyLimitItem.add(urgencyItem);
					}
					appStatus.setArrUrgencyLimitItem(arrUrgencyLimitItem);
					callback.OnRelationDiseasePestCompleted(true, true, mResult);
				}
			}
			
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		
	}
}
